<?php
require 'constants.php';

Class MY_Controller extends CI_Controller {
	
	
	protected $fb_cookie;
	protected $fb_appId;
	protected $fb_id;
	
	function __construct() {
		parent::__construct();
		
		$this->fb_appId = $this->config->item('fb_appId');
		$fb_secret = $this->config->item('fb_secret');
			
		$facebook = new Facebook(array(
			'appId'  => $this->fb_appId,
			'secret' => $fb_secret,
		));
		//if logged in, set the facebook id of the person as well as the access token
		if (get_cookie('fbsr_' . $this->fb_appId)) {
			$user = $facebook->getUser();
			if($user){
				$this->fb_id = $user;
			}
		}
		else{
			$this->fb_id = -1;
		}
	}
	
	//to check if the person is currently logged in
	function LoggedIn(){
		if($this->fb_id != -1){
			return true;
		}
		//Redirect to login page
		else {		
			$data['title'] = 'The Trove';
			$data['content_view'] = 'splash_view';
			$data['javascripts'] = array();
			$data['stylesheets'] = array('splash');
			$this->load_view($data, FALSE);
		
			return false;
		}
	}
	
	//Used to load content view. This function should be called whenever a page is displayed
	protected function load_view($ext_data, $include_top_bottom_bars = TRUE) {
		$data['appId']  = $this->fb_appId;
		$data['include_top_bar'] = $include_top_bottom_bars;
		$data['include_bottom_bar'] = $include_top_bottom_bars;
		$data['fb_id'] = $this->fb_id;

		$data = array_merge($data, $ext_data);		
		$this->load->view('template_view', $data);
	}
	
	
	function add_status_message($message){
		$status = $this->session->userdata('status_messages');
		if($status){
			$status[] = $message;
		}
		else{
			$status = array($message);
		}
		
		$this->session->set_userdata('status_messages', $status);
	}
}